package com.iotek.bookstore.web.controlle;

import com.iotek.bookstore.modle.entity.Address;
import com.iotek.bookstore.modle.service.serviceimpl.AddressBizImpl;
import net.sf.json.JSON;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/login/addressServlet")
public class AddressServlet extends CommonServlet {
    @Override
    public void doInsert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if(req.getParameter("addressId")==""){
            Address address=new Address();
            int userId=(Integer)req.getSession().getAttribute("userId");
            long addressId=Long.valueOf(String.valueOf(userId)+System.currentTimeMillis());
            address.setAddressId(addressId);
            address.setUserId(userId);
            address.setName(req.getParameter("name"));
            address.setSite(req.getParameter("address2"));
            address.setPostcode(req.getParameter("postcode"));
            address.setPhone(req.getParameter("phone"));
            address.setMobilePhone(req.getParameter("mobilePhone"));
            System.out.println("addressServlet----doInsert:打印收件人地址信息");
            System.out.println("收件人："+address.getName()+"--收件人地址："+address.getSite()+
                "---收件人邮政编码："+address.getPostcode()+"---电话："+address.getPhone()+"---手机："+address.getMobilePhone());
            new AddressBizImpl().insert(address);
            req.setAttribute("addressId",addressId);
            req.getRequestDispatcher("/login/createOrderServlet").forward(req,resp);
        }else if(req.getParameter("addressId")==null){
            Address address=new Address();
            int userId=(Integer)req.getSession().getAttribute("userId");
            long addressId=Long.valueOf(String.valueOf(userId)+System.currentTimeMillis());
            address.setAddressId(addressId);
            address.setUserId(userId);
            address.setName(req.getParameter("name"));
            address.setSite(req.getParameter("address2"));
            address.setPostcode(req.getParameter("postcode"));
            address.setPhone(req.getParameter("phone"));
            address.setMobilePhone(req.getParameter("mobilePhone"));
            System.out.println(req.getParameter("phone"));
            if(new AddressBizImpl().insert(address)==0){
                resp.getWriter().write("0");
            }else {
                resp.getWriter().write("1");
            }
        }else {
            long addressId=Long.valueOf(req.getParameter("addressId"));
            req.setAttribute("addressId",addressId);
            req.getRequestDispatcher("/login/createOrderServlet").forward(req,resp);
        }


    }

    @Override
    public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            long addressId=Long.valueOf(req.getParameter("addressId"));
            if(new AddressBizImpl().delete(addressId)==0){
                resp.getWriter().write("0");
            }else {
                resp.getWriter().write("1");
            }
    }

    @Override
    public void doUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            long addressId=Long.valueOf(req.getParameter("addressId"));
            int userId=(Integer)req.getSession().getAttribute("userId");
            String name=req.getParameter("name");
            String address=req.getParameter("address");
            String postcode=req.getParameter("postcode");
            String phone=req.getParameter("phone");
            String mobilePhone=req.getParameter("mobilePhone");
            Address address1=new Address(addressId,userId,name,address,postcode,phone,mobilePhone);
            System.out.println("信息来自--addressServlet---doUpdate:");
            System.out.println("地址ID："+address1.getAddressId()+"--用户 ID:"+address1.getName()+"--收件人地址："+address1.getSite()+
                "--收件人邮编："+address1.getPostcode()+"--收件人电话："+address1.getPhone()+"--收件人手机："+address1.getMobilePhone());
            if(new AddressBizImpl().update(address1)==0){
                resp.getWriter().write("0");
            }else {
                resp.getWriter().write("1");
            }
    }

    @Override
    public void doQuery(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int userId=(Integer)req.getSession().getAttribute("userId");
        System.out.println("信息来自AddressServlet:");
        System.out.println("用户ID："+userId);
        String type=req.getParameter("type");
        System.out.println("type:"+type);
        if(type.equals("initData")){
            List<Address>addressList=new AddressBizImpl().query(userId,"userId");
            req.setAttribute("address",addressList);
            req.getRequestDispatcher("address.jsp").forward(req,resp);
            for(Address address:addressList){
                System.out.println("信息来自AddressServlet:");
                System.out.println("地址ID："+address.getAddressId());
            }
        }else if(type.equals("doData")){
            long addressId=Long.valueOf(req.getParameter("addressId"));
            List<Address>addressList=new AddressBizImpl().query(addressId,"addressId");
            JSON json= JSONArray.fromObject(addressList);
            resp.getWriter().write(json.toString());
            System.out.println("信息来自AddressServlet:");
            System.out.println(json.toString());
        }else if(type.equals("queryAll")){
            List<Address>addressList=new AddressBizImpl().query(userId,"userId");
            JSON json=JSONArray.fromObject(addressList);
            System.out.println("AddressServlet----doQuery:");
            System.out.println(json.toString());
            resp.getWriter().write(json.toString());
        }

    }
}
